package com.xsomnus.spring_framework.edge.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xsomnus.spring_framework.edge.UserEdgeService;
import com.xsomnus.spring_framework.entity.User;
import com.xsomnus.spring_framework.service.IUserFundsLogService;
import com.xsomnus.spring_framework.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/26 0026 12:15
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 * {https://blog.csdn.net/hcmony/article/details/77850183}
 */
@Service
@AllArgsConstructor
@EnableTransactionManagement
public class UserEdgeServiceImpl implements UserEdgeService {

    private final IUserService iUserService;

    private final IUserFundsLogService iUserFundsLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean decrease(String userId, Integer modified)  {
        final boolean ret = iUserService.update(new UpdateWrapper<User>().setSql("version = version + 1, left_fee = left_fee - " + modified).eq("id", userId));
        iUserFundsLogService.saveFundsLog(userId, modified);
        if (userId.equalsIgnoreCase("jack")) {
            throw new RuntimeException("异常");
        }
        return ret;
    }

    @Override
    public boolean testPropagation() {
        return false;
    }
}
